export const TOKEN_KEY = 'User-Token';
export const PERMISSIONS_KEY = 'User-Permissions';
export const ROLES_KEY = 'User-Roles';
export const USER_KEY = 'User-Info';
export const USER_MENU_KEY = 'User-Menu';

/**
 * 设置sessionStorage
 * @param {string} key
 * @param {any} value
 */
export function setCache(key, value) {
  if (value == undefined || value == null) {
    window.sessionStorage.setItem(key, value);
  } else {
    window.sessionStorage.setItem(key, JSON.stringify(value));
  }
}

/**
 * 获取sessionStorage
 * @param {string} key
 * @returns {any}
 */
export function getCache(key) {
  if (window.sessionStorage.getItem(key) == 'undefined' || window.sessionStorage.getItem(key) == null) {
    return undefined;
  }
  return JSON.parse(window.sessionStorage.getItem(key));
}

/**
 * 清除指定sessionStorage
 * @param {string} key
 */
export function removeCache(key) {
  return window.sessionStorage.removeItem(key);
}

/**
 * 清除所有sessionStorage
 */
export function removeAll() {
  return window.sessionStorage.clear();
}

/**
 * 获取TOKEN
 */
 export function getToken() {
  return getCache(TOKEN_KEY)
}

/**
 * 设置TOKEN
 * @param {any} value
 */
export function setToken(token) {
  return setCache(TOKEN_KEY, token)
}

/**
 * 删除TOKEN
 */
export function removeToken() {
  return removeCache(TOKEN_KEY)
}